﻿using Compass.Maui.Services.FinishGoods;

namespace Compass.Maui.ViewModels.FinishGoods;

public partial class FinishGoodsCountsPageViewModel : BaseViewModel
{
    private readonly IFinishGoodsCountService _finishGoodsCountService;

    public FinishGoodsCountsPageViewModel(IFinishGoodsCountService finishGoodsCountService)
    {
        _finishGoodsCountService = finishGoodsCountService;
    }


    public ObservableRangeCollection<FinishedGoodsCountDto> FinishGoodsCountDtos { get; } = new();
    public ObservableRangeCollection<FinishedGoodsCountDto> FilterFinishGoodsCountDtos { get; } = new();

    [RelayCommand]
    private async Task GetFinishGoodsCountDtosAsync()
    {
        await IsBusyFor(async () =>
        {
            try
            {
                FinishGoodsCountDtos.ReplaceRange(await _finishGoodsCountService.GetAllWithFeeAsync(false));
                Filter();
            }
            catch (Exception e)
            {
                await Shell.Current.DisplayAlert("Error!", $"Unable to get finishgoodscounts:{e.Message}", "OK");
            }
        });
    }


    #region 搜索
    private string search;
    /// <summary>
    /// 搜索条件属性
    /// </summary>
    public string Search
    {
        get => search;
        set
        {
            search = value;
            OnPropertyChanged();
            Filter();
        }
    }
    private void Filter()
    {
        FilterFinishGoodsCountDtos.ReplaceRange(FinishGoodsCountDtos.Where(x => string.IsNullOrEmpty(Search) ||
                x.OdpNumber.ToLower().Contains(Search.ToLower()) ||
                x.Name.ToLower().Contains(Search.ToLower())));
    }
    #endregion
}